package com.wzy.server.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.*;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.contexts.SecurityContext;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.List;


@Configuration
@EnableSwagger2
public class Swagger2Config {

//    哪些包扫描文档
    @Bean
    public Docket createRestApi() {
//                                  文档类型
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())//文档
                .select()//扫描的包
                //为当前包下的controller生成api文档
                .apis(RequestHandlerSelectors.basePackage("com.wzy.server.controller"))
                .paths(PathSelectors.any())//任何路径
                .build()
                //添加登录认证
                .securitySchemes(securitySchemes())//设置请求头信息
                .securityContexts(securityContexts());//设置需要登录认证的路径
    }


//设置文档信息
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("在线办公接口文档")//标题
                .description("在线办公接口文档")//描述
                .contact(new Contact("wzy", "http:localhost:8081/doc.html", "1506465273@qq.com"))//作者信息
                .version("1.0")
                .build();
    }


//设置请求头信息
    private List<ApiKey> securitySchemes(){
        List<ApiKey> result = new ArrayList<>();
//                                         apikey的名字                 k                     v
        ApiKey apiKey = new ApiKey("Authorization","Authorization","header");
        result.add(apiKey);
        return result;
    }


//设置需要登录认证的路径
    private List<SecurityContext> securityContexts() {
        List<SecurityContext> result = new ArrayList<>();
        result.add(getContextByPath("/hello/.*"));//获取hello路径下所有的文件
        return result;
    }
//获取路径方法
    private SecurityContext getContextByPath(String pathRegex) {
        return SecurityContext.builder()
                .securityReferences(defaultAuth())//默认授权
                .forPaths(PathSelectors.regex(pathRegex))//指定路径
                .build();
    }
//获取默认授权方法
    private List<SecurityReference> defaultAuth() {
        List<SecurityReference> result = new ArrayList<>();
        //                                         授权范围                      范围全局                   描述
        AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything");
        AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];//准备数组
        authorizationScopes[0] = authorizationScope;//放入authorizationScope
        result.add(new SecurityReference("Authorization",authorizationScopes));//必须放数组
        return result;
    }





}
